package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.dto.SetmealDto;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.service.DishFlavorService;
import com.reggie.service.DishService;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐
 * @program: reggie
 * @author:
 * @create: 2022-09-29 21:47
 **/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    /*@Autowired
    private DishFlavorService dishFlavorService;*/

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Setmeal::getName,name);
        setmealService.page(setmealPage,wrapper);
        return R.success(setmealPage);

    }

    /**
     * 套餐的添加
     * setmeal和setmealDsih
     * */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.addSetmeal(setmealDto);
        return R.success("添加成功！");
    }

    /**
     * 套餐修改页面数据的回显
     * */
    @GetMapping("/{id}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<SetmealDto> show(@PathVariable Long id){
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getId,id);
        Setmeal setmeal = setmealService.getOne(wrapper);
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(setmealDishWrapper);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

    /**
     * 套餐页面数据的修改
     * 修改Setmeal表中的数据
     * 删除和添加SetmealDish表中的数据
     * */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功！");
    }
    /**
     * 套餐的停售和起售功能
     * @param status 已经修改后的参数，表示当前起售的状态。0为停售，1为起售
     * */
    @PostMapping("/status/{status}")
    public R<String> selling(@PathVariable int status,Long[] ids){
        Setmeal setmeal = new Setmeal();
        for (Long id : ids) {
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal   );
        }
        return R.success("修改状态成功");
    }
    /**
     * 套餐数据的删除
     * */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteSetmeal(Long[] ids){
        setmealService.deleteSetmeal(ids);
        return R.success("删除成功");
    }
    /**
     * 移动端显示套餐的数据
     *
     * */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#categoryId+_+#status")
    public R<List> list(Long categoryId,int status){

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }
    /**
     * 移动端获取套餐中的具体数据信息
     * 需要展示口味信息
     * @param id SetmealId
     * */
    @GetMapping("/dish/{id}")
    public R<List> dish(@PathVariable Long id){
        
        //根据setmeal的id获取setmealDish中的数据
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        return R.success(list);

    }
}
